package com.webserver.controller;

import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.vo.Article;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ArticleController {
    private static File articleDir;

    static {
        articleDir = new File(".articles");
        if (!articleDir.exists()) {
            articleDir.mkdirs();
        }
    }

    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理发表...");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        if (title == null || author == null || content == null) {
            File file = new File("./webapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }

        File articleFile = new File(articleDir, title + ".obj");

        if (articleFile.exists()) {
            File file = new File("./webapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }

        Article article = new Article(title, author, content);

        try (
                FileOutputStream fos = new FileOutputStream(articleFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos)
        ) {
            oos.writeObject(article);
            System.out.println("写出完毕！");

            File file = new File("./webapps/myweb/article_success.html");
            response.setEntity(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("发表处理完成！");
    }

    public void showAllArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始生成动态页面...");
        List<Article> articlesList = new ArrayList<>();
        File[] subs = articleDir.listFiles(f -> f.getName().endsWith(".obj"));
        for (File articleFile : subs) {
            try (
                    FileInputStream fis = new FileInputStream(articleFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                Article article = (Article) ois.readObject();
                articlesList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        articlesList.forEach(a -> System.out.println(a));

        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("    <meta charset=\"UTF-8\">");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("    <h1>文章列表</h1>");
        pw.println("    <table border=\"1\" bgcolor=\"black\">");
        pw.println("        <tr bgcolor=\"#f0fff0\">");
        pw.println("            <td align=\"center\">标题</td>");
        pw.println("            <td  align=\"center\">作者</td>");
        for (Article a : articlesList) {
            pw.println("        <tr bgcolor=\"#ffffe0\">");
            pw.println("            <td>" + a.getTitle() + "</td>");
            pw.println("            <td>" + a.getAuthor() + "</td>");
            pw.println("        </tr>");
        }
        pw.println("    </table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        System.out.println("写出完毕！");

        response.setContentType("text/html");


        System.out.println("生成动态页面完毕");
    }
}
